/**
 *	@author: Manimin Morga
 *	date created: January 6, 2006
 *	version: 2.0
 *	modified: July 24, 2006
 */
 
package text.ia;

import java.util.Vector;

import javax.swing.JOptionPane;

/**
 *	Accepts whole text file in String and returns 
 *	a Vector of Sentences in String form
 */
public class SentenceDetector implements SentenceDetectorInterface {

	//Contains the segmented sentences
	private Vector vSentences;

	public SentenceDetector() {
		vSentences = null;
	}

	/**
	 *	Segments the document using the next line character ['\n']
	 */
	protected void toSentencesByLineBreak(String strText) {

		String[] arrStringSentences = strText.replaceAll("[\\s]+"," ").trim().split("\n");
    	
    	for (int i = 0; i < arrStringSentences.length; i++)
 			vSentences.add(arrStringSentences[i].trim());

 		vSentences.trimToSize();
	}

	/**
	 *	First, the document is segmented by using the next line character ['\n'].
	 *	Afterwards, the document is segmented based on punctuation.
	 *	If a period is encountered, 
	 *		the word attached to the period is checked if it is an abbrev.
	 *		If it is an abbrev. then the word is skipped.
	 */
	protected void toSentencesByRegExp(String strText, char cTarget) {

		RegularExpressions re = new RegularExpressions(strText.replaceAll("[\\s]+"," ").trim(), cTarget);

		re.identifySentencesUsingRegularExpressions();
		
 		vSentences = re.getSentences();
	}

	public Vector toSentences(String strText, String strMethod, char cTarget) {

		vSentences = new Vector();
		
		if(strMethod.compareToIgnoreCase("Line Break") == 0) {
			toSentencesByLineBreak(strText);
		}else if(strMethod.compareToIgnoreCase("Regular Expression") == 0) {
			toSentencesByRegExp(strText, cTarget);
		}else {
			vSentences = null;
			
			//Error
			JOptionPane.showMessageDialog(null, "Sentence Detector Method: " 
				+ strMethod + " is not implemented!", "Error!", JOptionPane.ERROR_MESSAGE);
			System.err.println("Sentence Detector Method: " + strMethod + " is not implemented!");
		}
		
		return vSentences;
	}
}